/***************************************************************************************
 The copyright in this software is being made available under the BSD License, included
 below. This software may be subject to other third party rights,including patent rights,
 and no such rights are granted under this license.

 Regents of the Tsinghua University, Peking University, Zhejiang University, HKUST
 Tsinghua University, Peking University, Zhejiang University, HKUST 2011

 Copyright (c) 2011, Regents of the Tsinghua University, Peking University, Zhejiang
 University, HKUST. All Rights Reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
   * Neither the names of the copyright holders nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 OF SUCH DAMAGE.
***************************************************************************************/


#ifndef _BLOCK_H_
#define _BLOCK_H_

#include "global.h"

#define NUM_2D_TABLES 4
#define CODE2D_ESCAPE_SYMBOL 59

extern const int RF_COEFF_COST[64];


extern const char VLC_Golomb_Order[3][7][2];
extern const char MaxRun[3][7];
extern const char RefAbsLevel[19][26];

short MB_16[8][8], MB_16_temp[8][8];

void transform_B8( int** curr_blk );
void inv_transform_B8( int** curr_blk );
void quant_b8( int qp, int mode, int** curr_blk );
int  scanquant_B8( int qp, int mode, int b8, int** curr_blk, int scrFlag, int *cbp );

void transform_B16      ( int** curr_blk );
void quant_B16( int qp,  int mode, int** curr_blk );
int  scanquant_B16( int qp, int mode, int** curr_blk, int scrFlag, int *cbp );
void inv_transform_B16  ( int** curr_blk );

void transform_b4( int curr_blk[B4_SIZE][B4_SIZE] );
void quant_b4( int qp,int mode, int curr_blk[B4_SIZE][B4_SIZE],int y_left,int x_top );
void Dequant_b4( int qp, int curr_blk[B4_SIZE][B4_SIZE] );
int zigzag_scan_b4( int *ACLevel, int *ACRun, int curr_blk[B4_SIZE][B4_SIZE] );
void inv_transform_b4( int curr_blk[B4_SIZE][B4_SIZE] ) ;
int trans_quant_one_4x4( int curr_blk_4x4[B4_SIZE][B4_SIZE],int mode, int qp, int scrFlag, int b8, int b4, int *cbp,int *bcbp );

int find_sad_8x8( int iMode, int iSizeX, int iSizeY, int iOffX, int iOffY, int m7[MB_BLOCK_SIZE][MB_BLOCK_SIZE] );
int sad_hadamard( int iSizeX, int iSizeY, int iOffX, int iOffY, int m7[MB_BLOCK_SIZE][MB_BLOCK_SIZE] );
int write_luma_coeff_b8( int b8, int intra );

int write_luma_coeff_b8_qcoder( int b8 );
int write_chroma_coeff_b8_qcoder( int b8 );

void get_inter_pred_y_block( int block, int mode, macroblock_t * currMB );

#ifdef RDO_Q
double est_luma_coeff_b8_qcoder( int b8, int intra ) ;
int est_chroma_coeff_b8_qcoder( int b8, int intra );
int est_luma_coeff_b4_qcoder( int b8, int b4, int intra ) ;
#endif

// zrs 2012-01-10
int write_block_coeff8x8( int b8 );
int write_block_coeff4x4( int b8, int b4 );
int trans_scanquant_b4 ( int qp, int mode,int block8x8, int block4x4, int **curr_blk, int ACLevel_4x4[17], int ACRun_4x4[17] );
#endif
